<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>编辑个人设置-老师</title>
	<link rel="stylesheet" href="css/left.css">
	<link rel="stylesheet" href="css/common.css">
	<link rel="stylesheet" href="css/header.css">
	<link rel="stylesheet" href="css/per_edit.css">
	<style>
		
	</style>
</head>
<body>
<!-- 头部信息 -->
<div class="header">
	<div class="header-logo">
		<div class="header-portrait">
<?php
$img_logo = "images/touxiang.jpg";

include_once("database.php");

if(isset($_COOKIE["tea_number"])) {

	$username = $_COOKIE["tea_number"];

	$selecttea = "select * from teachers where tea_number='$username'";
	$result = mysql_query($selecttea) or die("查询老师信息表头像失败：".mysql_error()."<br/>");

	$row = mysql_fetch_array($result,MYSQL_ASSOC);

	$tea_name = $row["tea_name"];
	$job_company = $row["job_company"];
	$tea_phone = $row["tea_phone"];
	$tea_email = $row["tea_email"];
	$address = $row["address"];
	$tea_province = $row["tea_province"];
	$tea_city = $row["tea_city"];
	$tea_county = $row["tea_county"];
	$tea_sex = $row["tea_sex"];
	$postal_code = $row["postal_code"];
	$teaper_signature = $row["teaper_signature"];
	$img_logo = $row["tea_touxiang"];


	if($img_logo == NULL) {
		$img_logo = "images/touxiang.jpg";
	} else {
		$img_logo = $row["tea_touxiang"];
	}
}
?>
			<?php echo "<img src='{$img_logo}' alt='logo' id='logo'>";?>	
		</div>
		<div class="header-text">
<?php 
$name = "你的昵称";

if(isset($_COOKIE["tea_number"])) {

	$username = $_COOKIE["tea_number"];

	$selecttea = "select tea_name from teachers where tea_number='$username'";
	$result = mysql_query($selecttea) or die("查询老师信息表头像失败：".mysql_error()."<br/>");

	$row = mysql_fetch_array($result,MYSQL_ASSOC);

	$name = $row["tea_name"];

	if($name == NULL) {
		$name = "你的昵称";
	} else {
		$name = $row["tea_name"];
	}

	
}
?>
			<?php echo "<h2>{$name}</h2>";?>
<?php
if(isset($_COOKIE["tea_number"]) && $_COOKIE["tea_number"] != NULL) {

	$username = $_COOKIE["tea_number"];

	echo "<div class='adm_text'><span>欢迎<span class='color'>{$username}</span>管理员登录当前的系统!</span>";
	echo "&nbsp;&nbsp;&nbsp;&nbsp;";
	echo "<a href=\"logout.php?action=logout\" class='color'>退出登录</a></div>";
} else {

	echo "<a href=\"register.php\" class='a color'>注册</a>&nbsp;<a href=\"login.php\" class='a'>登录</a><br/>";
}
?>
			<p>这位老师很忙，什么也没有留下...</p>
		</div>
		<div class="header-news">
			<a href="" class="news_a">
				<?php echo "<img src='images/news.png' alt='图片' class='news'>";?>
			</a>
		</div>
	</div>
</div>

<!-- 中间内容 -->
<div class="content clearfloat">
	<!-- 左边内容 -->
	<div class="left_content">
		<ul class="left_nav" id="left_ul">
			<li><a href="notice.php" class="li_a">
				<span class="text">
					<?php echo "<img src='images/notice.png' alt='图片' class='left_img1'";?>&nbsp;
					<span>动&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;态</span>
				</span>
				<canvas id="canvas_index1" width="18" height="48"></canvas>	
			</a></li>
			<li><a href="addcourse.php" class="li_a">
				<span class="text">
					<?php echo "<img src='images/course.png' alt='图片' class='left_img2'";?>
					<span>创建课程</span>
				</span>
				<canvas id="canvas_index2" width="18" height="48"></canvas>
			</a></li>
			<li><a href="mycourse.php" class="li_a">
				<span class="text">
					<?php echo "<img src='images/mycourse.png' alt='图片' class='left_img3'";?>
					<span>我的课程</span>
				</span>
				<canvas id="canvas_index3" width="18" height="48"></canvas>
			</a></li>
			<li><a href="index3.php" class="li_a">
				<span class="text">
					<?php echo "<img src='images/correct.png' alt='图片' class='left_img4'";?>
					<span>批改作业</span>
				</span>
				<canvas id="canvas_index4" width="18" height="48"></canvas>
			</a></li>
			<li class="active_nav"><a href="per_setup.php" class="li_a">
				<span class="text">
					<?php echo "<img src='images/setup.png' alt='图片' class='left_img6'";?>
					<span>个人设置</span>
				</span>
				<canvas id="canvas_index6" width="18" height="48"></canvas>
			</a></li>
		</ul>
	</div>

	<!-- 右边内容 -->
	<div class="center_content">
		<a href="per_setup.php" class="per">返回</a>
		<form action="" method="post" enctype="multipart/form-data" class="per_infor">
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label for="tea_name">昵称:</label>
<?php 	
	echo "<input type='text' name='tea_name' id='tea_name' value='{$name}'><br/>";
?>			<label for="job_company">工作单位:</label>
<?php 
	echo "<input type='text' name='job_company' id='job_company' value='{$job_company}'><br/>";
?>
			<div class="touxiang">
				<div id="image-holder">
<?php
$file_img = "images/touxiang.jpg";

if(isset($_COOKIE["tea_number"])) {

	$username = $_COOKIE["tea_number"];

	$selecttea = "select tea_touxiang from teachers where tea_number='$username'";
	$result = mysql_query($selecttea) or die("查询老师信息表头像失败：".mysql_error()."<br/>");

	$row = mysql_fetch_array($result,MYSQL_ASSOC);

	$file_img = $row["tea_touxiang"];

	if($file_img == NULL) {
		$file_img = "images/touxiang.jpg";
	} else {
		$file_img = $row["tea_touxiang"];
	}
}
?>
					<?php echo "<img src='{$file_img}' alt='图片' id='file_img'>";?>
				</div>
				<a href="javascript:void(0);" class="input" id="huan">
					换一换<input type="file" id="upfile" name="upfile" value="换一换">
				</a>&nbsp;&nbsp;&nbsp;&nbsp;
				<a href="javascript:void(0);" class="input">
					上传头像<input type="file" id="infile" name="infile">
				</a>
			</div>
			
			<label for="tea_phone">联系电话:</label>
<?php
	echo "<input type='text' name='tea_phone' id='tea_phone' value='{$tea_phone}'>";
?>
			<label>所在地区:</label>
<?php
	echo "<select name='tea_province' id='province'>
			<option value='省' selected='selected'>省</option>
			<option value='广西壮族自治区'>广西壮族自治区</option>
			<option value='广东省'>广东省</option>
		</select>";
	echo <<<script_select
	<script>
		var province = document.getElementById("province");
		var option = province.children;
		var len = option.length;
		for(var i = 0; i < len; i++) {
			option[i].selected = "";

			if(option[i].innerHTML == '{$tea_province}') {
				option[i].selected = "selected";
			} else {
				option[0].selected = "selected";
			}
		}
	</script>
script_select;
?>
<?php
	echo "<select name='tea_city' id='city'>
			<option value='市' selected='selected'>市</option>
			<option value='南宁市'>南宁市</option>
			<option value='桂林市'>桂林市</option>
			<option value='广州市'>广州市</option>
			<option value='深圳市市'>深圳市市</option>
			<option value='东莞市'>东莞市</option>
		</select>";
	echo <<<script_select
	<script>
		var city = document.getElementById("city");
		var option = city.children;
		var len = option.length;
		for(var i = 0; i < len; i++) {
			option[i].selected = "";

			if(option[i].innerHTML == '{$tea_city}') {
				option[i].selected = "selected";
			} else {
				option[0].selected = "selected";
			}
		}
	</script>
script_select;
?>
<?php
	echo "<select name='tea_county' id='county'>
			<option value='区县' selected='selected'>区县</option>
			<option value='阳朔县'>阳朔县</option>
			<option value='青秀区'>青秀区</option>
		</select><br/>";
	echo <<<script_select
	<script>
		var county = document.getElementById("county");
		var option = county.children;
		var len = option.length;
		for(var i = 0; i < len; i++) {
			option[i].selected = "";

			if(option[i].innerHTML == '{$tea_county}') {
				option[i].selected = "selected";
			} else {
				option[0].selected = "selected";
			}
		}
	</script>
script_select;
?>

			<label for="tea_email">电子邮箱:</label>
<?php
	echo "<input type='text' name='tea_email' id='tea_email' value='{$tea_email}'>";
?>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label>性别:</label>
<?php 
	echo "<span id='sex'>
				<input type='radio' name='tea_sex' value='男' checked='checked' class='sex'>&nbsp;&nbsp;男&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
				<input type='radio' name='tea_sex' value='女' class='sex'>&nbsp;&nbsp;女<br/>
			</span>";
	echo <<<script_radio
	<script>
		var sex = document.getElementById("sex");
		var input = sex.children;
		var len = input.length;
		for(var i = 0; i < len; i++) {
			input[i].checked = "";

			if(input[i].value == '{$tea_sex}') {
				input[i].checked = "checked";
			} else {
				input[0].checked = "checked";
			}
		}
	</script>
script_radio;
?>

			<label for="address">联系地址:</label>
<?php
	echo "<input type='text' name='address' id='address' value='{$address}'>";
?>
			<label for="postal_code">邮政编码:</label>
<?php
	echo "<input type='text' name='postal_code' id='postal_code' value='{$postal_code}'><br/>";
?>
			<label for="teaper_signature">个性签名:</label>
<?php
	echo "<textarea name='teaper_signature' id='teaper_signature' cols='30' rows='10'>{$teaper_signature}</textarea><br/>";
?>
  			<input type="button" name="submit" value="确定" class="btn btn_sub" onclick="window.open('per_setup.php','_self')">
  			<input type="reset" name="quxiao" value="取消" class="btn">
		</form>
	</div>

	<!-- 右侧栏 -->
	<?php
		include_once("right_content.php");
	?>
</div>

<!-- 加载左边导航的样式 -->
<?php
	include_once("nav_style.php");
?>

<!-- 加载底部信息 -->
<?php
	include_once("footer.php");
?>


<?php
if(isset($_COOKIE["tea_number"])) {
	$tea_number = $_COOKIE["tea_number"];

	if(isset($_POST["submit"]) && $_POST["submit"] == "确定") {
		foreach($_FILES as $key => $value) {
			// echo "{$key}的值为:<br/>";
			foreach ($_FILES[$key] as $ke => $valu) {
				// echo "{$ke}: {$valu}<br/>";
			}
		}

		if(!empty($_FILES[$key]["name"])) {//判断是否选择了文件上传
			//指定上传文件路径以及名字
			$pathname = "./imgs/".$_FILES[$key]["name"];

			$tea_touxiang = "imgs/".$_FILES[$key]["name"];
		
			$result = move_uploaded_file($_FILES[$key]["tmp_name"], $pathname);					
		} else {

			$selecttea = "select tea_touxiang from teachers where tea_number='$tea_number'";
			$result = mysql_query($selecttea) or die("查询老师信息表头像失败：".mysql_error()."<br/>");

			$row = mysql_fetch_array($result,MYSQL_ASSOC);

			$tea_touxiang = $row["tea_touxiang"];

			if($tea_touxiang == "images/touxiang.jpg" || $tea_touxiang == "imgs/" || $tea_touxiang == "") {
				
				$tea_touxiang = "images/touxiang.jpg";
			} 
		}


		$tea_name = $_POST["tea_name"];
		$job_company = $_POST["job_company"];
		$tea_phone = $_POST["tea_phone"];
		$tea_email = $_POST["tea_email"];
		$address = $_POST["address"];
		$tea_province = $_POST["tea_province"];
		$tea_city = $_POST["tea_city"];
		$tea_county = $_POST["tea_county"];
		$tea_sex = $_POST["tea_sex"];
		$postal_code = $_POST["postal_code"];
		$teaper_signature = $_POST["teaper_signature"];

		$updatesql = "update teachers set tea_name='$tea_name',job_company='$job_company',tea_phone='$tea_phone',
			tea_email='$tea_email',address='$address',tea_province='$tea_province',tea_city='$tea_city',
			tea_county='$tea_county',tea_sex='$tea_sex',postal_code='$postal_code',
			teaper_signature='$teaper_signature',tea_touxiang='$tea_touxiang' where tea_number='$tea_number'";
		
		@mysql_query($updatesql) or die("修改老师信息表失败：".mysql_error()."<br/>");
	
		$selecttea = "select tea_touxiang from teachers where tea_number='$tea_number'";
		$result = mysql_query($selecttea) or die("查询老师信息表头像失败：".mysql_error()."<br/>");

		$row = mysql_fetch_array($result,MYSQL_ASSOC);

		$img = $row["tea_touxiang"];

		echo <<<script_img
		<script>
			var img_logo = document.getElementById("logo");
			var file_img = document.getElementById("file_img");

			img_logo.src = "{$img}";
			file_img.src = "{$img}"; 
		</script>
script_img;
	}
}
?>

<script src="js/jquery-3.2.1.js"></script>
<script>
    $("#upfile").on('change', function () {
        if (typeof (FileReader) != "undefined") {

            var image_holder = $("#image-holder");
            image_holder.empty();

            var reader = new FileReader();
            reader.onload = function (e) {
                $("<img />", {
                    "src": e.target.result,
                    "class": "thumb-image"
                }).appendTo(image_holder);
            };
            image_holder.show();
            reader.readAsDataURL($(this)[0].files[0]);
        } else {
            alert("你的浏览器不支持FileReader.");
        }

    });

    $("#infile").on('change', function () {
        if (typeof (FileReader) != "undefined") {

            var image_holder = $("#image-holder");
            image_holder.empty();

            var reader = new FileReader();
            reader.onload = function (e) {
                $("<img />", {
                    "src": e.target.result,
                    "class": "thumb-image"
                }).appendTo(image_holder);
            };
            image_holder.show();
            reader.readAsDataURL($(this)[0].files[0]);
        } else {
            alert("你的浏览器不支持FileReader.");
        }
    });

  	function getback(){
  		window.histroy.back();
  	}
</script>
</body>
</html>